package com.wangmin.intercept.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.wangmin.intercept.MainActivity;
import com.wangmin.intercept.R;
import com.wangmin.intercept.base.BaseActivity;
import com.wangmin.intercept.dialog.LoadingDialog;
import com.wangmin.intercept.init.SdkInit;
import com.wangmin.intercept.service.SocketService;
import com.wangmin.intercept.view.CircleImageView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wm on 2017/4/22.
 */
public class LoginActivity extends BaseActivity {
    @Bind(R.id.tl_custom)
    Toolbar tlCustom;
    @Bind(R.id.about_appbar)
    AppBarLayout aboutAppbar;
    @Bind(R.id.civ_head)
    CircleImageView civHead;
    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.et_pass)
    EditText etPass;
    @Bind(R.id.bt_login)
    Button btLogin;
    @Bind(R.id.ll_root)
    LinearLayout llRoot;
    private LoadingDialog loadingDialog;
    public static final String ServiceActionLogin = "com.wangmin.action.service.login";
    private BroadcastReceiver broadcastReceiver;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setSupportActionBar(tlCustom);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setMsg("正在登录,请稍后...");
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int code = intent.getIntExtra("loginResult",-1);
                String msg = intent.getStringExtra("msg");
                if(code == 1){
                    showToastMsg("登录成功");
                    startActivity(MainActivity.class,null);
                    finish();
                }else{
                    showToastMsg("登录失败"+msg);
                }
                SdkInit.getUiHandle().post(new Runnable() {
                    @Override
                    public void run() {
                        loadingDialog.dismiss();
                    }
                });
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ServiceActionLogin);
        registerReceiver(broadcastReceiver,intentFilter);
        final Intent intent = new Intent(this, SocketService.class);
        startService(intent);
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(checkNull()){
                   loadingDialog.show();
                   Intent intent1 = new Intent(SocketService.LOGIN);
                   intent1.putExtra("ip",etUsername.getText().toString());
                   intent1.putExtra("pass",etPass.getText().toString());
                   sendBroadcast(intent1);
                }
            }
        });
    }

    private boolean checkNull() {
        String username = etUsername.getText().toString();
        if (TextUtils.isEmpty(username)) {
            showToastMsg("ip不能为空");
            return false;
        }
        String password = etPass.getText().toString();
        if (TextUtils.isEmpty(password)) {
            showToastMsg("密码不能为空");
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(broadcastReceiver!=null){
            unregisterReceiver(broadcastReceiver);
        }
    }
}
